home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
vc
/
pro3
/
mv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-06
|
4KB
|
153 lines
===========================================================================
BBS: The Abacus * HST/DS * Potterville, MI
Date: 06-04-93 (09:08) Number: 163
From: BOB STOUT Refer#: NONE
To: ALL Recvd: NO
Subj: mv.c Conf: (36) C Language
---------------------------------------------------------------------------
Since someone asked - from the next SNIPPETS:
/*
** mv.c -- move or rename files or directories
** updated for multiple files, 5 jul 92, rlm
** placed in the public domain via C_ECHO by the author, Ray McVay
**
** modified by Bob Stout, 28 Mar 93
** modified by Bob Stout, 4 Jun 93
**
** uses file_copy from SNIPPETS file WB_FCOPY.C
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
/* For portability, make everything look like MSC 6 */
#if defined(__TURBOC__)
#include <dir.h>
#define _dos_findfirst(f,a,b) findfirst(f,b,a)
#define find_t ffblk
#define _A_SUBDIR FA_DIREC
#define attrib ff_attrib
#else /* assume MSC/QC */
#include <direct.h>
#endif
/*
** Tell 'em they messed up
*/
void help(char *s)
{
puts("usage: mv <oldname [...]> <newname|newdir>");
printf("error: %s\n", s);
}
/*
** Simple directory test
*/
isdir(char *path)
{
struct find_t f;
/* "Raw" drive specs are always directories */
if (':' == path[1] && '\0' == path[2])
return 1;
return (_dos_findfirst(path, _A_SUBDIR, &f) == 0 &&
(f.attrib & _A_SUBDIR));
}
/*
** Use rename or copy and delete
*/
int mv(char *src, char *dest)
{
int errcount = 0;
char buf[FILENAME_MAX];
const char *generr = "ERROR: mv - couldn't %s %s %s\n";
if (':' == dest[1] && *dest != *getcwd(buf, FILENAME_MAX))
{
if (file_copy(src, dest))
{
printf(generr, "move", src, dest);
++errcount;
}
else if (unlink(src))
{
printf(generr, "delete", src, "");
++errcount;
}
}
else
{
if (rename(src, dest))
{
printf(generr, "rename", src, dest);
++errcount;
}
}
return errcount;
}
/*
** Enter here
*/
int main(int argc, char **argv)
{
int src, errcount = 0;
char target[FILENAME_MAX];
puts("mv 1.3 (4 jun 93) - Ray L. McVay/Bob Stout");
if (argc < 3)
help("Not enough parameters");
/*
** Handle cases where target is a directory
*/
else if (isdir(argv[argc -1]))
{
for (src = 1; src < argc - 1; src++)
{
char termch;
strcpy(target, argv[argc - 1]);
termch = target[strlen(target) - 1];
if ('\\' != termch && ':' != termch)
strcat(target, "\\");
if (strrchr(argv[src], '\\'))
strcat(target, strrchr(argv[src], '\\') + 1);
else if (argv[src][1] == ':')
strcat(target, argv[src] + 2);
else strcat(target, argv[src]);
errcount += mv(argv[src], target);
}
}
/*
** Nothing left except 2 explicit file names
*/
else if (argc == 3)
errcount += mv(argv[1], argv[2]);
return errcount;
}
--- QM v1.00
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20